package com.leetcode.string;

public class AddBinary {

    public static void main(String[] args) {
        String a = "11";
        String b = "1";
        AddBinary addBinary = new AddBinary();
        String res = addBinary.addBinary(a, b);
        System.out.println(res);
    }
    public String addBinary(String a, String b) {
        int aLen = a.length();
        int bLen = b.length();
        int i = 0;
        char ch1;
        char ch2;
        int val1;
        int val2;
        int high = 0;
        int low = 0;
        String resStr = "";
        while(true) {
            if (aLen - 1 - i >= 0) {
                ch1 = a.charAt(aLen - 1 -i);
            } else {
                ch1 = 0;
            }
            if (bLen - 1 - i >= 0) {
                ch2 = b.charAt(bLen - 1 -i);
            } else {
                ch2 = 0;
            }
            if (ch1 == 0 && ch2 == 0) {
                break;
            }
            if (ch1 != 0) {
                val1 = Integer.parseInt(String.valueOf(ch1));
            } else {
                val1 = ch1;
            }
            if (ch2 != 0) {
                val2 = Integer.parseInt(String.valueOf(ch2));
            } else {
                val2 = ch2;
            }
            low = (val1 + val2 + high) % 2;
            high = (val1 + val2 + high) / 2;
            resStr = low + resStr;
            i++;
        }
        if (high != 0) {
            resStr = high + resStr;
        }
        return resStr;
    }
}
